package com.generator.common.api.bean;

import com.generator.common.util.ConfigReadUtil;
import com.generator.common.util.SqlReservedWords;
import com.generator.common.util.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础javabean字段类
 *
 * @author
 * @create 2018-03-26 20:40
 **/
@Setter
@Getter
@ToString
@Accessors(chain = true)
public class Filed {
    public static final String LOW_BOOLEAN = "boolean";
    public static final String UP_BOOLEAN = "Boolean";
    public static final String DB_BIT = "BIT";
    public static final String IS = "is_";

    public static String ESCAPES = ConfigReadUtil.getYmlString("filed.escapes","`");
    private static boolean MATCH_KEYWORD = ConfigReadUtil.getYmlBoolean("generate.matchkeyword",true);
    private String type;
    private String originalName;//原始名称
    private String escapesName;//转义后名称
    private String camelName;//驼峰名
    private String backCamelName;//首字母不大写驼峰
    private String upperCaseUnderline;//全部大写并含有下划线  实际上是把数据库字段转大写
    private boolean primaryKey;//是否主键
    private boolean connectAttribute;//是否是其他表的连接属性 如订单表中的 user_id user_name
    private String comment;
    private String interval;//间距
    private boolean acquired;
    private List<String> annotations = new ArrayList<>();
    /**
     * 新增属性符号
     */
    private String  symbol;

    public Filed(){

    }

    /**
     * 构造最简单字段属性
     * @param type
     * @param originalName
     */
    public Filed(String type, String originalName){
        this(type,originalName,false);
    }

    /**
     * 可构造使用实际列名字段
     * @param type
     * @param originalName
     * @param useOriginalName
     */
    public Filed(String type, String originalName,boolean useOriginalName) {
        this(type,originalName,useOriginalName,false);
    }

    /**
     * 完全构造
     * @param type 字段类型
     * @param originalName 实际列名字段
     * @param useOriginalName 是否为实际列名字段
     * @param connectAttribute 是否为其他表的连接id
     */
    public Filed(String type, String originalName,boolean useOriginalName,boolean connectAttribute) {
        this.type = type;
        this.originalName = originalName;
        if(MATCH_KEYWORD) {
            setEscapesName(this.originalName);
        }
        this.upperCaseUnderline = originalName.toUpperCase();
        if(LOW_BOOLEAN.equals(type) || UP_BOOLEAN.equals(type) || DB_BIT.equals(type)){
            originalName = originalName.replace(IS,"");
        }
        this.camelName = StringUtils.underlineToCamel(originalName,true);
        this.connectAttribute = connectAttribute;
        this.backCamelName = useOriginalName ? originalName : camelName.substring(0,1).toLowerCase()+camelName.substring(1);
    }

    public Filed setEscapesName(String originalName) {
        this.escapesName = SqlReservedWords.containsWord(originalName) ? ESCAPES+originalName+ESCAPES : originalName;
        return this;
    }

    public Filed addAnnotation(String annotation) {
        this.annotations.add(annotation);
        return this;
    }

    public Filed setInterval(int length){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append(" ");
        }
        this.interval = sb.toString();
        return this;
    }

}
